package io.renren.modules.generator.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.generator.entity.PositionEntity;
import io.renren.modules.generator.service.PositionService;
import io.renren.modules.generator.vo.PositionVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 物料位置信息
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-11 00:13:08
 */
@RestController
@RequestMapping("generator/position")
public class PositionController {
    @Autowired
    private PositionService positionService;

    /**
     * 列表
     */
    @RequestMapping("/listForSelect")
//    @RequiresPermissions("generator:position:list")
    public R listForSelect(){
        List<PositionVo> response = new ArrayList<>();
        List<PositionEntity> page = positionService.selectList(Condition.empty());
        page.stream().forEach(x->{
            PositionVo vo = new PositionVo();
            vo.setValue(x.getPsCode());
            vo.setLabel(x.getAreaCode());
            response.add(vo);
        });
        return R.ok().put("data", response);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:position:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = positionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:position:info")
    public R info(@PathVariable("id") Long id){
			PositionEntity position = positionService.selectById(id);

        return R.ok().put("position", position);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:position:save")
    public R save(@RequestBody PositionEntity position){
			positionService.insert(position);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:position:update")
    public R update(@RequestBody PositionEntity position){
			positionService.updateById(position);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:position:delete")
    public R delete(@RequestBody Long[] ids){
			positionService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
